from app import db
from datetime import datetime

class ProductCategory(db.Model):
    __tablename__ = 'product_category'

    category_id = db.Column(db.String(36), primary_key=True, comment='分类ID')
    category_name = db.Column(db.String(50), nullable=False, comment='分类名称')
    parent_id = db.Column(db.String(36), default='0', comment='父分类ID')
    order_num = db.Column(db.Integer, default=0, comment='显示顺序')
    shop_id = db.Column(db.String(36), nullable=False, comment='所属店铺ID')
    create_time = db.Column(db.DateTime, default=datetime.now, comment='创建时间')
    update_time = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now, comment='更新时间')

    def to_dict(self):
        return {
            'categoryId': self.category_id,
            'categoryName': self.category_name,
            'parentId': self.parent_id,
            'orderNum': self.order_num,
            'shopId': self.shop_id,
            'createTime': self.create_time.strftime('%Y-%m-%d %H:%M:%S') if self.create_time else None,
            'updateTime': self.update_time.strftime('%Y-%m-%d %H:%M:%S') if self.update_time else None
        }